﻿using MediatR;
using T.Materials.Domain.Dto;
using T.Materials.Domain.Model;
using T.Materials.Read.API.ApplicationRead.Command.MaterialRequisitionCommand;
using T.Materials.Read.API.ApplicationRead.Command.MaterialStoreCommand;
using T.Materials.Repository.MaterialsImpl;
using T.Materials.Repository.MaterialsInterface;

namespace T.Materials.Read.API.ApplicationRead.CommandHandler.MaterialRequisitionCommandHandler
{
    public class ShowCollectingItemsCommandHandler : IRequestHandler<ShowCollectingItemsCommand, List<StorageMaterialsDto>>
    {
        /// <summary>
        /// 存储物资领用存储仓储
        /// </summary>
        protected readonly IStorageMaterialsRepository storageMaterialsRepository;

        /// <summary>
        /// 物资领用仓储
        /// </summary>
        protected readonly IMaterialRequisitionRepository materialRequisitionRepository;

        /// <summary>
        /// 物资与入库仓储
        /// </summary>
        protected readonly IMaterialStoreRepository materialStoreRepository;

        /// <summary>
        /// 国家仓储接口
        /// </summary>
        protected readonly ICountryRepository countryRepository;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="storageMaterialsRepository">存储物资领用存储仓储</param>
        /// <param name="materialRequisitionRepository">物资领用仓储</param>
        /// <param name="materialStoreRepository">物资与入库仓储</param>
        /// <param name="countryRepository">国家仓储接口</param>
        public ShowCollectingItemsCommandHandler(IStorageMaterialsRepository storageMaterialsRepository, IMaterialRequisitionRepository materialRequisitionRepository, IMaterialStoreRepository materialStoreRepository, ICountryRepository countryRepository)
        {
            this.storageMaterialsRepository = storageMaterialsRepository;
            this.materialRequisitionRepository = materialRequisitionRepository;
            this.materialStoreRepository = materialStoreRepository;
            this.countryRepository = countryRepository;
        }

        /// <summary>
        /// 处理显示指定仓储物资信息命令
        /// </summary>
        /// <param name="request">显示指定仓储物资信息命令</param>
        /// <param name="cancellationToken">取消标记</param>
        /// <returns>返回指定ID的物资入库仓储列表</returns>
        public async Task<List<StorageMaterialsDto>> Handle(ShowCollectingItemsCommand request, CancellationToken cancellationToken)
        {
            var list = (from a in await storageMaterialsRepository.GetAllAsync()
                       join b in await materialRequisitionRepository.GetAllAsync()
                       on a.MaterialRequisitionId equals b.MaterialRequisitionId
                       join c in await materialStoreRepository.GetAllAsync()
                       on a.MaterialStoreId equals c.MaterialStoreId
                       join d in await countryRepository.GetAllAsync()
                       on c.CountryId equals d.CountryId
                       where !a.IsDeleted && !b.IsDeleted && !c.IsDeleted && a.MaterialRequisitionId == request.MaterialRequisitionId
                       select new StorageMaterialsDto
                       {
                           StorageMaterialsId = a.StorageMaterialsId,
                           MaterialRequisitionId = a.MaterialRequisitionId,
                           MaterialStoreId = a.MaterialStoreId,
                           QuantityReceived = a.QuantityReceived,
                           NameOfGoods = c.NameOfGoods,
                           MaterialStoreType = c.MaterialStoreType,
                           Material = c.Material,
                           Specifications = c.Specifications,
                           CountryId = c.CountryId,
                           CountryName=d.CountryName,
                           Number = c.Number,
                           Title=b.Title,
                           CollectionDate=b.CollectionDate,
                       }).ToList();
            return list;
        }
    }
}
